package actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import amushen.base.BaseAction;
import utils.*;
public class SystemUtilAction extends BaseAction {

	public ActionForward execSql(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(!"shennan".equals(get("password"))){
			request.setAttribute("info", "error password");
			return new ActionForward("/main/util/systemUtil.jsp");
		}
		SystemUtil su=new SystemUtil();
		List list=su.execSql(get("sql"));
		if(list==null)request.setAttribute("info", "execute failed");
		else if(list.size()==0)request.setAttribute("info", "success");
		else request.setAttribute("info", "result:");
		request.setAttribute("result", list);
		//history
		if(list!=null){
			List history=new ArrayList();
			if(request.getSession().getAttribute("sqlHistory")!=null)
				history=(List)request.getSession().getAttribute("sqlHistory");
			boolean isExist=false;
			String sss=get("sql");
			for(int i=0;i<history.size();i++)
				if(sss.equalsIgnoreCase((String)history.get(i))){
					isExist=true;
					break;
				}
			if(!isExist)history.add(get("sql"));
			request.getSession().setAttribute("sqlHistory", history);
		}
		return new ActionForward("/main/util/systemUtil.jsp");
	}
}
